<template>
  <div>
    <div class="btn" style="margin: 0 0 10px 20px;">
      <el-button @click="toAdd" type="primary">新增</el-button>
    </div>

    <div class="table">
      <el-table element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)" :data="categoryData" style="width: 100%">
        <el-table-column type="index" label="序号" width="100" align="center">
        </el-table-column>

        <el-table-column label="名称" width="100" align="center">
          <template v-slot="scope">
            <div>
              <div>{{ scope.row.name }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="no" label="排序号" width="100" align="center">
          <template v-slot="scope">
            <div>
              <div>{{ scope.row.no }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="描述" width="750px" align="center">
          <template v-slot="scope">
            <div>
              <div>{{ scope.row.description }}</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <div>
              <el-link @click="toEdit(scope.row)" style="font-size: 13px; margin-right: 5px;" type="success"
                :underline="false">编辑</el-link>
              <el-link @click="toDelete(scope.row.id)" style="font-size: 13px;" type="danger"
                :underline="false">删除</el-link>
            </div>
          </template>

        </el-table-column>
      </el-table>
    </div>

    <el-dialog @close="resetField" width="50%" :title="title" :visible.sync="visible">
      <el-form :model="form" :rules="rules" ref="myForm" :label-position="labelPosition">
        <!-- {{ form }} -->
        <el-form-item prop="name" label="栏目名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="no" label="栏目序号" :label-width="formLabelWidth">
          <el-input v-model="form.no" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="introduce" label="栏目介绍" :label-width="formLabelWidth" style="margin-top: 40px;">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button @click="toSave" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from '@/utils/request'
export default {
  data() {
    return {
      categoryData: [],
      title: '新增栏目',
      visible: false,
      rules: {
        name: [
          { required: true, message: '请输入栏目名称', trigger: 'blur' }
        ]
      },
      form: {},
      labelPosition: 'right',
      formLabelWidth: '80px',

    }
  },
  methods: {
    async getCategory() {
      let res = await get('category/findAll')
      this.categoryData = res.data
      console.log(res);

      console.log(this.categoryData);

    },
    // 新增 弹出模态框
    toAdd() {
      this.visible = true;
      this.form = {}
      this.title = '新增栏目'
    },
    // 去保存
    toSave() {
      this.$refs.myForm.validate(async (valid) => {
        if (valid) {
          let res = await post('/category/saveOrUpdate', this.form)
          console.log(this.form);

          if (res.status === 200) {
            this.getCategory();
            this.$message({
              type: 'success',
              message: res.message
            })
          } else {
            this.$message.error(res.message)
          }
          this.visible = false
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetField() {
      this.$refs.myForm.resetFields();
    },
    // 编辑轮播图
    toEdit(row) {
      this.visible = true;
      this.title = '编辑轮播图信息'
      this.form = { ...row };
    },
    // 删除栏目
    toDelete(id) {
      console.log(id);
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await get('/category/deleteById', { id })
        if (res.status === 200) {
          this.getCategory()
          // console.log(res);
          this.$message({
            type: 'success',
            message: res.message
          });
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  },
  created() {
    this.getCategory()
  },
}
</script>